package com.sgb.kjwl.view.ui.activity.packers;

import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityTeamApplyJoinBinding;
import com.sgb.kjwl.model.entity.packers.ApplyCompanyEntity;
import com.sgb.kjwl.model.entity.packers.MyCompanyEntity;
import com.sgb.kjwl.viewmodel.packers.TeamApplyJoinModel;
import com.sgb.link.router.PathTeam;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickerHelper;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.List;

/**
 * @author: 王彬臣
 * @date: 2021/9/6
 * @Description: 工队申请加盟
 */
@Route(path = PathTeam.TeamApplyJoinActivity)
public class TeamApplyJoinActivity extends BaseActivity<TeamApplyJoinModel, ActivityTeamApplyJoinBinding> implements ItemInputTextView.OnWholeItemClickListener {
    @Autowired(name = RouterKey.NO,desc = "公司no")
    String no;
    @Autowired(name = RouterKey.NAME,desc = "公司名")
    String name;
    List<CommonPopEntity> levelList;
    private OptionsPickerView<CommonPopEntity> levelPicker;
    List<CommonPopEntity> typeList;
    private OptionsPickerView<CommonPopEntity> typePicker;

    List<MyCompanyEntity> companyList;
    private OptionsPickerView<MyCompanyEntity> companyPicker;
    ApplyCompanyEntity mEntity;
    String myCompanyNo,myCompanyName;

    @Override
    protected int layoutId() {
        return R.layout.activity_team_apply_join;
    }

    @Override
    protected void createViewModel() {
        mViewModel=new ViewModelProvider(this).get(TeamApplyJoinModel.class);
    }

    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        mBinding.setOnClick(this);
        mBinding.itemMyCompany.setOnWholeItemClickListener(this);
        mBinding.llNation.setOnClickListener(this);
        mBinding.itemType.setOnWholeItemClickListener(this);
        mBinding.ivNation.setOnClickListener(this);
        mBinding.itemApplyCompany.setContent(name);
        mViewModel.ldLevel.observe(this,data->{
            levelList=data;
            if(levelList.size()>0){
                if ("全部".equals(levelList.get(0).getTypeName())) {
                    levelList.remove(0);
                }
                initLevelPick();
                levelPicker.show();
            }

        });
        mViewModel.ldType.observe(this,data->{
            typeList=data;
            initTypePick();
            typePicker.show();
        });

        mViewModel.ldCompany.observe(this,data->{
            companyList=data;
            initCompanyPick();
            companyPicker.show();
        });
        mViewModel.ldResult.observe(this,result->{
            if(result){
                finish();
            }
        });
        int mCompanyType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        myCompanyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        myCompanyName=SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NAME,"");
        mEntity=new ApplyCompanyEntity();
        if(mCompanyType ==1){
            //如果是企业
            mBinding.itemMyCompany.setContent(myCompanyName);
            mEntity.setCompanyNo(myCompanyNo);
            mEntity.setCompanyName(myCompanyName);
        }

        mEntity.setLeagueCompanyName(name);
        mEntity.setLeagueCompanyNo(no);
    }

    private void initTypePick() {
        typePicker= PickerHelper.with(this).getCommonPicker("选择会员类型",typeList,new OnOptionsSelectListener(){
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                CommonPopEntity unitEntity=typeList.get(options1);
                mBinding.itemType.setContent(unitEntity.getTypeName());
                mEntity.setMemberType(unitEntity.getNo());
            }
        });
    }

    public void initLevelPick(){
       levelPicker= PickerHelper.with(this).getCommonPicker("选择会员等级",levelList,new OnOptionsSelectListener(){
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                CommonPopEntity unitEntity=levelList.get(options1);
                mBinding.itemLeve.setText(unitEntity.getTypeName());
                mEntity.setLeagueLevel(unitEntity.getTypeName());
                mBinding.ivNation.setVisibility(View.VISIBLE);
            }
        });
    }

    public void initCompanyPick(){
        companyPicker= PickerHelper.with(this).getCommonPicker("选择我的单位",companyList,new OnOptionsSelectListener(){
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                MyCompanyEntity myCompanyEntity=companyList.get(options1);
                mBinding.itemMyCompany.setContent(myCompanyEntity.getName());
                mEntity.setCompanyName(myCompanyEntity.getName());
                mEntity.setCompanyNo(myCompanyEntity.getNo());
            }
        });
    }

    @Override
    public void OnWholeItemClick(View view) {
        if(view.getId()==R.id.item_my_company){
            if(mViewModel.ldCompany.getValue()!=null&&mViewModel.ldCompany.getValue().size()>0){
                companyPicker.show();
            }else{
                mViewModel.getCompany();
            }
        }else if(view.getId()==R.id.item_type){
            if(mViewModel.ldType.getValue()!=null&&mViewModel.ldType.getValue().size()>0){
                typePicker.show();

            }else{
                mViewModel.getMemberType();
            }
        }

    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.submit){
            if(ClickUtils.isDoubleClick()){
                return;
            }
            if(TextUtils.isEmpty(mBinding.itemMyCompany.getContent())){
                MToast.showToast(this,"请选择我的单位");
                return;
            }
            if(mEntity.getCompanyNo().equals(no)){
                MToast.showToast(this,"不能申请加盟自己的企业");
                return;
            }
            if(TextUtils.isEmpty(mBinding.itemLinkName.getContent())){
                MToast.showToast(this,"请输入联系人");
                return;
            }else if(mBinding.itemLinkName.getContent().length()<2){
                MToast.showToast(this,"联系人信息字段长度不能少于两位");
                return;
            }
            if(TextUtils.isEmpty(mBinding.itemLinkPhone.getContent())){
                MToast.showToast(this,"请输入联系电话");
                return;
            }

            if(!CheckUtils.checkMobile(mBinding.itemLinkPhone.getContent())){
                MToast.showToast(this,"请输入正确联系电话");
                return;
            }

            if(TextUtils.isEmpty(mBinding.itemType.getContent())){
                MToast.showToast(this,"请选择会员类型");
               return;
            }
            mEntity.setContacts(mBinding.itemLinkName.getContent());
            mEntity.setPhone(mBinding.itemLinkPhone.getContent());
            DialogHelper.showProgressDialog(this, "加载中").setCanceledOnTouchOutside(true);
            mViewModel.apply(mEntity);
        } else if (v.getId() == R.id.ll_nation) {
            if(mViewModel.ldLevel.getValue()!=null&&mViewModel.ldLevel.getValue().size()>0){
                levelPicker.show();
            }else{
                mViewModel.getMemberLevels();
            }
        }else if (v.getId() == R.id.iv_nation) {
            mBinding.itemLeve.setText("");
            mEntity.setLeagueLevel("");
            mBinding.ivNation.setVisibility(View.GONE);
        }
    }
}
